package com.prs.crm.dao.cdxh.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.courser.hibernate.QueryExample;
import org.courser.ui.PaginationSupport;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cdxh.CrmCustomerDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.CrmCustomer;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.hr.Person;

public class CrmCustomerDaoImpl extends BaseDaoImpl<CrmCustomer> implements
		CrmCustomerDao {

	@Override
	public Collection<CrmCustomer> getAllCrmCustomer() {
		// TODO Auto-generated method stub
		return (Collection<CrmCustomer>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(CrmCustomer.class);
				return criteria.list();
			}
			
		});
	}

	@Override
	public Collection<CrmCustomer> getCrmCustomerByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<CrmCustomer> getCustomerByLay() {
		return null;
	}

	@Override
	public CrmCustomer getCustomerBycusCode(final String Code) {
		return (CrmCustomer)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(CrmCustomer.class);
				        	criteria.add(Restrictions.eq("cusCode", Code));		        	
				        	List<cd_Product> result = criteria.list();
				        	
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}


}
